package com.demo.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Create with Intellij idea
 * User:鲁智明
 * Date:2018/9/19
 * Time:22:27
 */
@RestController
public class SessionController {
    @Value("${server.port}")
    private String port;
    @RequestMapping("/addSession")
    public String addSession(HttpServletRequest request, String key, String value) {
        //默认不传值为true ，没有sessionId  则会创建sessionId
        HttpSession session = request.getSession(true);
        session.setAttribute("userName", "小明");
        return "添加成功,port:"+port;
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request, String key) {
        String userName = null;
        try {
            HttpSession session = request.getSession(false);
            userName = (String) session.getAttribute("userName");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return userName+",port:"+port;


    }
}

